package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.zhgd.annotation.ZhjdDataFilter;
import com.ruoyi.zhgd.util.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhgd.dto.CementMatchListDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NCementMatchBo;
import com.ruoyi.zhgd.domain.vo.NCementMatchVo;
import com.ruoyi.zhgd.domain.NCementMatch;
import com.ruoyi.zhgd.mapper.NCementMatchMapper;
import com.ruoyi.zhgd.service.INCementMatchService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.ruoyi.common.helper.LoginHelper.getLoginUser;

/**
 * 水稳拌和楼生产级配Service业务层处理
 *
 * @author cj
 * @date 2023-10-08
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NCementMatchServiceImpl implements INCementMatchService {

    private final NCementMatchMapper baseMapper;

    /**
     * 查询水稳拌和楼生产级配
     */
    @Override
    public NCementMatchVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询水稳拌和楼生产级配列表
     */
    @Override
    public TableDataInfo<NCementMatchVo> queryPageList(NCementMatchBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NCementMatch> lqw = buildQueryWrapper(bo);
        Page<NCementMatchVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水稳拌和楼生产级配列表
     */
    @Override
    public List<NCementMatchVo> queryList(NCementMatchBo bo) {
        LambdaQueryWrapper<NCementMatch> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NCementMatch> buildQueryWrapper(NCementMatchBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NCementMatch> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBaseCode()), NCementMatch::getBaseCode, bo.getBaseCode());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectId()), NCementMatch::getProjectId, bo.getProjectId());
        lqw.eq(StringUtils.isNotBlank(bo.getStationCode()), NCementMatch::getStationCode, bo.getStationCode());
        lqw.eq(StringUtils.isNotBlank(bo.getMatchCode()), NCementMatch::getMatchCode, bo.getMatchCode());
        lqw.eq(StringUtils.isNotBlank(bo.getPavingLevel()), NCementMatch::getPavingLevel, bo.getPavingLevel());
        lqw.eq(bo.getGravel1() != null, NCementMatch::getGravel1, bo.getGravel1());
        lqw.eq(bo.getGravel2() != null, NCementMatch::getGravel2, bo.getGravel2());
        lqw.eq(bo.getGravel3() != null, NCementMatch::getGravel3, bo.getGravel3());
        lqw.eq(bo.getGravel4() != null, NCementMatch::getGravel4, bo.getGravel4());
        lqw.eq(bo.getGravel5() != null, NCementMatch::getGravel5, bo.getGravel5());
        lqw.eq(bo.getGravel6() != null, NCementMatch::getGravel6, bo.getGravel6());
        lqw.eq(bo.getPowder1() != null, NCementMatch::getPowder1, bo.getPowder1());
        lqw.eq(bo.getPowder2() != null, NCementMatch::getPowder2, bo.getPowder2());
        lqw.eq(bo.getWater() != null, NCementMatch::getWater, bo.getWater());
        lqw.like(StringUtils.isNotBlank(bo.getGravel1Name()), NCementMatch::getGravel1Name, bo.getGravel1Name());
        lqw.like(StringUtils.isNotBlank(bo.getGravel2Name()), NCementMatch::getGravel2Name, bo.getGravel2Name());
        lqw.like(StringUtils.isNotBlank(bo.getGravel3Name()), NCementMatch::getGravel3Name, bo.getGravel3Name());
        lqw.like(StringUtils.isNotBlank(bo.getGravel4Name()), NCementMatch::getGravel4Name, bo.getGravel4Name());
        lqw.like(StringUtils.isNotBlank(bo.getGravel5Name()), NCementMatch::getGravel5Name, bo.getGravel5Name());
        lqw.like(StringUtils.isNotBlank(bo.getGravel6Name()), NCementMatch::getGravel6Name, bo.getGravel6Name());
        lqw.like(StringUtils.isNotBlank(bo.getPowder1Name()), NCementMatch::getPowder1Name, bo.getPowder1Name());
        lqw.like(StringUtils.isNotBlank(bo.getPowder2Name()), NCementMatch::getPowder2Name, bo.getPowder2Name());
        lqw.eq(bo.getCreator() != null, NCementMatch::getCreator, bo.getCreator());
        lqw.eq(bo.getCreateDate() != null, NCementMatch::getCreateDate, bo.getCreateDate());
        lqw.eq(bo.getUpdater() != null, NCementMatch::getUpdater, bo.getUpdater());
        lqw.eq(bo.getUpdateDate() != null, NCementMatch::getUpdateDate, bo.getUpdateDate());
        return lqw;
    }

    /**
     * 新增水稳拌和楼生产级配
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(NCementMatch bo) {
        bo.setCreateDate(new Date());
        bo.setUpdateDate(new Date());
        bo.setUpdater(getLoginUser().getUserId());
        NCementMatch add = BeanUtil.toBean(bo, NCementMatch.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改水稳拌和楼生产级配
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(NCementMatch bo) {
        bo.setUpdateDate(new Date());
        bo.setUpdater(getLoginUser().getUserId());
        NCementMatch update = BeanUtil.toBean(bo, NCementMatch.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NCementMatch entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水稳拌和楼生产级配
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(String ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteById(ids) > 0;
    }

    @Override
    @ZhjdDataFilter
    public List<NCementMatch> list(Map<String, Object> params) {
        QueryWrapper<NCementMatch> queryWrapper = new QueryWrapper<NCementMatch>();
        if (ObjectUtils.isNotEmpty(params.get("matchCode"))) {
            queryWrapper.eq("match_code", params.get("matchCode").toString());
        }
        if (ObjectUtils.isNotEmpty(params.get("projectId"))) {
            queryWrapper.like("project_id", params.get("projectId").toString());
        }
        if (ObjectUtils.isNotEmpty(params.get("baseCode"))) {
            queryWrapper.like("base_code", params.get("baseCode").toString());
        }
        if (ObjectUtils.isNotEmpty(params.get("stationCode"))) {
            queryWrapper.like("station_code", params.get("stationCode").toString());
        }
        if (ObjectUtils.isNotEmpty(params.get(ZhjdDataFilter.DATA_FILTER_SQL))) {
            queryWrapper.apply(params.get(ZhjdDataFilter.DATA_FILTER_SQL).toString());
        }
        queryWrapper.orderByAsc("station_code,match_code");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public PageUtils queryPage(Page page, Map<String, Object> params) {
        List<CementMatchListDto> list = baseMapper.queryPages(page, params);
        return new PageUtils(page.setRecords(list));
    }

}
